<template>
    <div class="components_edit_modal">
        <el-dialog width="500px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="dictionary" :model="dictionary" :rules="rules" label-width="95px">

                <el-form-item label="字典类型：" prop="type">
                    <el-input v-model.trim="dictionary.type"></el-input>
                </el-form-item>

                <el-form-item label="字典名称：" prop="dictValue">
                    <el-input v-model.trim="dictionary.dictValue"></el-input>
                </el-form-item>

                <el-form-item label="字典描述：" prop="description">
                    <el-input v-model.trim="dictionary.description"></el-input>
                </el-form-item>

                <el-form-item label="字典值：" prop="dictKey">
                    <el-input v-model.trim="dictionary.dictKey"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addDictionary">确 定</el-button>
                <el-button @click="state = false"  size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    export default {
        data() {

            return {
                state: false,
                title: '',
                type:'',
                dictionary:{
                    type: null,
                    dictValue: null,
                    dictKey: null,
                    description:null
                },
                rules: {
                    type: [
                        { required: true, message: '请输入字典类型', trigger: 'blur'}
                    ],
                    dictValue: [
                        { required: true, message: '请输入字典名称', trigger: 'blur'}
                    ],
                    description: [
                        // { required: true, message: '请输入字典描述', trigger: 'blur'}
                    ],
                    dictKey: [
                        { required: true, message: '请输入字典值', trigger: 'blur'}
                    ]
                },
            }
        },
        created() {

        },
        mounted() {},
        methods: {
            showDialog(val){

                if(this.$refs['dictionary']!==undefined){
                    this.$refs['dictionary'].resetFields();
                }

                if(val){
                    for(var key in val){
                       this.dictionary[key] = val[key];
                    }
                    this.title = "编辑字典";
                    this.type = "edit";
                }else{
                    this.title = "添加字典";
                    this.type = "add";
                }

                this.state = true;
            },
            addDictionary(){
                let vm = this;
                vm.$refs.dictionary.validate((valid) => {
                    if (valid) {

                        let submission =  ''; //提交方式

                        //选择提交方式
                        if(vm.type == 'edit'){
                            submission = 'put';
                        }else{
                            submission = 'post';
                        }
                        // 提交
                        this.axios({
                            method: submission,
                            url: api.dictionary.dictHandle,
                            data: vm.dictionary
                        }).then((response) => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.dictionaryTable.$emit('vuetable:refresh');
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        })
                    }else{
                        return false;
                    }
                });
            }
        },


    };
</script>
<style scoped>
.dialog-footer{ text-align:center; }
.dialog-footer button{ margin: 0 10%; }
</style>